package cn.tedu.vrd07.controller;

import cn.tedu.vrd07.entity.Category;
import cn.tedu.vrd07.mapper.CategoryMapper;
import cn.tedu.vrd07.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/category/")
public class CategoryController {
    @Autowired(required = false)
    CategoryMapper mapper;
    @Autowired(required = false)
    ProductMapper productMapper;
    @Value("${dirPath}")
    private String dirPath;
    @RequestMapping("select")
    public List<Category> select(){
        return mapper.select();
    }
    @RequestMapping("delete")
    public void delete(int id){
        mapper.delete(id);
    }
    @RequestMapping("insert")
    public void insert(Category category){
        mapper.insert(category);
    }
    @RequestMapping("deleteAll")
    public String deleteAll(int id){
        //查询出和此分类相关的所有作品图片的url
        List<String> urlAll=productMapper.selectUrlsById(id);
        //遍历所有图片路径
        for (String url: urlAll) {
            String filePath=dirPath+url;
            new File(filePath).delete();
        }
        //返回值代表的是生效的行数
       int row= productMapper.deleteByCid(id);
       mapper.delete(id);
       return "删除了分类和"+row+"个作品！";
    }
}
